import { createStore, Store, useStore as useVuexStore } from "vuex";
import { IRootState, IStoreType } from "./types";
//注册组件
import login from "./login/login";
import system from "./mian/system/system";
import { getPageListData } from "@/service/main/system/system";

const store = createStore<any>({
  state() {
    return {
      entirRole: [],
    };
  },
  mutations: {
    changeEntireRole(state, list) {
      state.entirRole = list;
    },
  },
  actions: {
    async getInitialDataAction({ commit }) {
      //请求菜单数据
      const roleResult = await getPageListData("/menu/list", {
        offset: 0,
        size: 100,
      });
      const { list: roleList } = roleResult.data;
    
      commit("changeEntireRole", roleList);
    },
  },
  modules: {
    login,
    system,
  },
});

export function setupStore() {
  store.dispatch("login/loadLocalLogin");
  store.dispatch('getInitialDataAction')
}

export function useStore(): Store<IStoreType> {
  return useVuexStore();
}

export default store;
